Skip to content

Let builds share a compile cache at the same time - #505

Open
dimensionscape wants to merge 4 commits into
mainfrom
feature/nervous-chebyshev-5ab4fa
Open

dimensionscape wants to merge 4 commits into
mainfrom
feature/nervous-chebyshev-5ab4fa

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

For 6.0.3. Builds that share one HXCPP_COMPILE_CACHE broke each other when they ran at the same time. This showed up while running test/regression/Cppia* against a shared cache and building a profiling host into it at once: a release host linked another build's HXCPP_DEBUGGER library and failed on the debugger symbols, another linked but crashed on every cppia workload, and a third failed with LNK1104: cannot open file ...hxcpp_sqlite.lib. Two commits, each with a Changes.md line.

Cached libraries (829f0ee)

A group cached with asLibrary (hxcpp_runtime, hxcpp-cppia, hxcpp_sqlite and others) was linked to <cache>/<project>/lib/<prefix>_<project>.lib, named only by the toolchain and the project. Builds with other defines compile other objects for those groups and linked them into the same file. One after the other, each build linked it again. At the same time, one linked the other's library between linking its own and linking the program: a -D HXCPP_DEBUGGER host and a plain one, built at once against a warm cache, failed in 3 of 3 rounds with unresolved __hxcpp_dbg_* and hx::sExecutionTrace, the same symbols defined twice, or LNK1104.

  • The linker names a library in the cache by a hash of its object names (which already hash source, flags and compiler) and of the tool, flags and libraries it is linked with. Each set of objects gets a library of its own. Builds with the same objects share one, which is linked again when an object is newer than it, as before.
  • It is linked under a name of the build's own and renamed into place. Windows renames onto no file that is there, so when the one there is up to date, another build linked the same objects first and this build drops its own. A stale one is moved aside and replaced. While another link has it open, the build links its own copy, deletes it on exit, and a later build replaces the stale one.

Cached objects and precompiled headers (2861ad1)

Builds with the same defines compile the same cached objects and make the same cached precompiled header, and each compiler wrote them in place. Two identical scriptable cppia hosts built at once from an empty cache both failed on hxcpp.h's header: one could not write hxcpp.obj while the other's cl.exe did ("Could not create PCH", C1083), deleted its half-made header, and the other then found no hxcpp.pch. The debugger and plain hosts above, which share the pcre2, zlib and other objects that HXCPP_DEBUGGER does not change, failed from an empty cache with LNK1104 on an object the other was still writing.

  • A cached object is compiled under a name of the build's own and renamed into place. One already there is kept when it would be reused anyway (not older than its precompiled header, nor than its source in a group that respects timestamps). A failed compile deletes only its own file, where it used to delete the cached one, which another build may just have made.
  • A cached precompiled header is made in a directory of the build's own, with its MSVC object, and the directory is renamed into place, so the two always arrive together. The cache no longer makes that directory empty in advance.
  • PathManager.mkdir no longer fails when another build makes the directory between its check and its make, which builds starting on an empty cache hit as "Could not create compiler cache directory".
  • The temp name, rename and replace helpers live in CompileCache, and the library code uses them too.

Stock hxcpp has all of this code unchanged, in 4.3.2 and on master. I found no report of it upstream.

Tests

Run on Windows x64 with MSVC 14.50 and Haxe 4.3.7:

  • test/compile_cache/shared_lib.sh (new, in test.yml) builds a cached library that returns VALUE in folders of its own with one cache. Before: a build with another VALUE changed the first build's library, the first build again linked it again, and in two runs 10 and 11 of 12 builds run four at a time failed with LNK1104 or LNK1181, while one printed another build's VALUE. After: all of it passes, including a library linked again in its place after its object was removed.
  • test/compile_cache/same_objects.sh (new, in test.yml) builds a cached group with a precompiled header, its sources in one shared folder, in four folders at once from an empty cache, three times. Before: all 12 builds failed. After: all 12 print 36, each having made the header and compiled all nine files, and nothing is left half made. With -D emscripten (emsdk 3.1.74, a clang precompiled header) all 12 build and print 36 under node.
  • test/compile_errors/check.sh passes with the cache and without.
  • By hand: a build that found its library older than its object while another process held it open linked its own copy and printed the right value, and the next build replaced it.
  • Two identical scriptable cppia hosts at once from an empty cache, and a HXCPP_DEBUGGER host with a plain one from empty and from warm (every library removed before each of three rounds), all built and ran a cppia script. An unchanged rebuild links nothing.
  • The C++ build tool type-checks (haxe compile-cpp.hxml --no-output).

gcc on Linux and Apple clang are untested here, so the new CI steps will be their first run.

Overlap with #495

This merges cleanly with main. Merged after this, #495 conflicts in two hunks of Compiler.precompile: its .dates check belongs in the new complete test, and its .dates file goes into the build's own directory (made) before CompileCache.putInPlace, not into dir after it. #495's records of what each build used work with the new library names.

A file group cached with asLibrary, as hxcpp_runtime, hxcpp-cppia and
hxcpp_sqlite are, is linked into <cache>/<project>/lib, named only by
the toolchain's object folder and the project. Builds with other
defines, as a cppia host built with HXCPP_DEBUGGER and one without,
compile other objects for those groups, and linked them into the same
file. One after the other, each build found the other's objects in the
.hash record and linked the library again. At the same time, one could
link the other's library between linking its own and linking the
program. On Windows with MSVC the debugger host failed on unresolved
__hxcpp_dbg_* symbols and hx::sExecutionTrace, the plain host on the
same symbols defined twice, and either could fail to open a library
(LNK1104) while the other's lib.exe wrote it. With a warm cache, three
rounds of the two hosts built at once failed both builds every time.
Stock hxcpp names the library the same way, in 4.3.2 and on master.

The linker now names a library in the cache by a hash of its objects'
names, which already hash each object's source, flags and compiler, and
of the tool, flags and libraries it is linked with. A build with other
objects gets a library of its own, and builds with the same objects
share one, linked again as before when an object is newer than it, as
one trimmed from the cache and compiled again is.

The library is linked under a name of the build's own beside it and
renamed into place, so no build links one half written, and a failed
link leaves nothing behind. Windows renames onto no file that is there,
and deletes none another link has open. So when the rename fails and
the library there is up to date, another build linked the same objects
and put it there first, and this build drops its own. One older than
the objects is deleted and the rename tried again, and while another
link has it open, this build links its own copy, deletes it on exit,
and leaves the replacing to a later build. Libraries under the old
names are no longer used and are left for trimming.

Tests: test/compile_cache/shared_lib.sh, run in test.yml, builds a
cached library that returns VALUE in folders of its own with one cache.
On Windows x64 with MSVC 14.50 and Haxe 4.3.7, before this change a
build with another VALUE changed the first build's library, the first
build again linked it again, and in two runs 10 and 11 of 12 builds
run four at a time failed with LNK1104 or LNK1181, while one printed
another build's VALUE each time. After it, all of it passes, including
a library linked again in its place after its object was removed. By
hand, a build that found its library older than its object while
another process held it open linked its own copy and printed the right
value, and the next build replaced it. Two scriptable cppia hosts, with HXCPP_DEBUGGER and without, built
at the same time against one cache with its libraries removed before
each of three rounds, linked their libraries and ran a cppia script
each time, and an unchanged rebuild linked nothing.
Builds that share a compile cache with the same defines, as two
projects built at once do, compile the same cached objects and make the
same cached precompiled header: their names hash the source, flags and
compiler, not the build. Each compiler wrote them in place, so from an
empty cache both builds compiled every shared file into the same path.
On Windows with MSVC, two identical scriptable cppia hosts built at once
both failed on hxcpp.h's precompiled header: one could not write
hxcpp.obj while the other's cl.exe did ("Could not create PCH", C1083),
deleted its half made one, and the other then found no hxcpp.pch. A
debugger host and a plain one, which share the pcre2, zlib and other
objects that HXCPP_DEBUGGER does not change, failed with LNK1104 on an
object the other build was still writing. Stock hxcpp writes them in
place too, in 4.3.2 and on master.

An object in the cache is now compiled under a name of the build's own
beside it and renamed to its name. A precompiled header in the cache is
made in a directory of the build's own, with its MSVC object, and the
directory is renamed to its name, so the two always arrive together,
and the cache no longer makes that directory empty in advance. When the
rename fails because another build put the same one there first, as
Windows renames onto nothing that is there and POSIX onto no directory
with files in it, the one there is kept if it will do: an object not
older than its precompiled header, or a header with its object. One
that will not do is moved aside and replaced, trying for 30 seconds
while another build has it open. A failed compile deletes only its own
file, where it deleted the cached one, which another build may just
have made. Cached libraries take the same helpers from CompileCache.

PathManager.mkdir also failed when another build made the directory
between its check and its make, which builds starting on an empty
cache hit as "Could not create compiler cache directory".

Tests: test/compile_cache/same_objects.sh, run in test.yml, builds a
cached group with a precompiled header, its sources in one shared
folder, in four folders at once from an empty cache, three times. On
Windows x64 with MSVC 14.50 and Haxe 4.3.7, before this change all 12
builds failed (C1083 on the header and on objects, and the cache
directory). After it, all 12 printed 36, each having made the header
and compiled all nine files, and nothing was left half made. With
-D emscripten (emsdk 3.1.74), which makes a clang precompiled header,
all 12 built and printed 36 under node. shared_lib.sh and
compile_errors/check.sh, with the cache and without, still pass. Two
identical scriptable cppia hosts built at once from an empty cache, and
a HXCPP_DEBUGGER host with a plain one, built and ran a cppia script.
The build tool reads file dates to the second, and links a library
again only for an object newer than it. On the Linux CI runner a build
takes under 100 ms, so after the test removed the first build's object,
the build compiled it again in the same second as the library was
linked, and did not link the library again, which failed the check
that it does. The object is compiled from the same source and flags, so
the library was still right. The test now waits a second first, as a
trimmed object compiled again later would be.

Tests: test/compile_cache/shared_lib.sh passes on Windows x64 with MSVC
14.50. It failed only on Linux 64 in CI, where the build is fast enough.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant